/*
 * @Author: dyt
 * @Date: 2018-07-04 09:56:30
 * @Last Modified by: simpledyt
 * @Last Modified time: 2018-12-06 18:26:19
 */
import * as api from '../service/api'
import {
  GET_MENU,
  GET_USER_INFO
} from './mutation-types'
import cookie from '@/utils/cookie'
export default {
  async getMenu ({ commit, state }) {
    commit(GET_MENU)
  },
  async getUserInfo ({ commit }) {
    try {
      let res = await api.getUserInfo()

      // 判断用户是否经过风险评测，并将相应标志位添加到用户信息中
      let riskTest = await api.getUserRiskInfo()
      let userInfo = res.data
      if (!riskTest.data.data) {
        userInfo.riskTest = false
      } else {
        userInfo.riskTest = true
      }
      commit(GET_USER_INFO, res.data)
      return res.data
    } catch (e) {
      console.log(e)
    }
  },
  /**
   * 更新本地用户信息
   * @param {*} param0
   * @param {用户信息} newUserInfo
   */
  updateUserInfo ({ commit, state }, newUserInfo) {
    commit(GET_USER_INFO, newUserInfo)
  },
  async loginOut ({ commit }) {
    try {
      await api.loginOut({
        token: cookie.get('device_id')
      })
      let clearMe = {
        me: {
          sysUser: {}
        }
      }
      cookie.remove('device_id')
      commit(GET_USER_INFO, clearMe)
      window.location.reload()
    } catch (e) {
      console.log(e)
    }
  }
}
